<?php

namespace seecms\controller;

use seecms\lib\builder\button\PageButton;
use seecms\lib\builder\control\TextareaControl;
use seecms\lib\builder\modal\Dialog;
use seecms\lib\builder\query\SelectQuery;
use seecms\See;
use seecms\SeeLang;

class UserRole extends Resource
{

    protected $table_name = 'see_user_role';

    protected $dictionary_name = 'user_role';

    protected function pageTitle(): string
    {
        return __('user_role.page_title');
    }

    public function query(): array
    {
        $users = See::table()->user_role->column('name', 'id');

        return [
            SelectQuery::init('name')->setOptions($users)->setPlaceholder(SeeLang::user('role_name')),
        ];
    }

    public function columns(): array
    {
        return [
            'id.100',
            'name.140',
            'menu',
            'rule',
            'description',
            'created_at',
        ];
    }

    public function actions(): array
    {
        return [
            PageButton::init('role_grant')
                ->setUrl(auth_route('permission/roleGrant'))
                ->setDialog(Dialog::init('role_grant')->areaLarge()),
            'edit',
            'del',
        ];
    }

    protected function controls(): array
    {
        return [
            'name',
            TextareaControl::init('description')->setRequired(),
        ];
    }

    public function delete(): array
    {
        $id = See::request()->param('id');

        if ($id == 1) {
            return $this->fail(SeeLang::user('delete_role'));
        }

        return parent::delete();
    }
}